package com.wtwd.campus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.dao.GradeMapper;
import com.wtwd.campus.dao.SchoolMapper;
import com.wtwd.campus.entity.Grade;
import com.wtwd.campus.entity.School;
import com.wtwd.campus.entity.SysOrg;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.model.vo.SchoolVO;
import com.wtwd.campus.service.SchoolService;
import com.wtwd.campus.service.SysOrgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mjy
 * @date 2021/06/11
 * @description
 */
@Service
public class SchoolServiceImpl implements SchoolService {

    @Resource
    private SchoolMapper schoolMapper;

    @Resource
    private GradeMapper gradeMapper;

    @Resource
    private SysOrgService sysOrgService;

    @Override
    public Integer getSchoolCountByOrgCode(String orgCode) {
        return schoolMapper.getSchoolCountByOrgCode(orgCode);
    }

    @Override
    public IPage<SchoolVO> queryPage(IPage page, School school) {
        return schoolMapper.queryList(page, school);
    }

    /**
     * 1.如果存在年级信息不可删除
     *
     * @param id
     * @return
     */

    @Transactional
    @Override
    public ResultVO deleteSchoolById(Integer id) {
        Integer count = new LambdaQueryChainWrapper<>(gradeMapper)
                .eq(Grade::getSchoolId, id)
                .count();
        if (count > 0) {
            return ResultVO.error(RespCode.SCHOOL_EXIST_GRADE);
        }
        ResultVO resultVO = sysOrgService.deleteById(id);
        if (RespCode.SUCCESS.getCode() != resultVO.getCode()) {
            return ResultVO.error(RespCode.ERROR);
        }
        schoolMapper.deleteById(id);
        return ResultVO.ok();
    }

    /**
     * 1.将学校纳入组织
     *
     * @param school
     * @return
     */
    @Transactional
    @Override
    public ResultVO createSchool(School school) {
        SysOrg sysOrg = new SysOrg();
        sysOrg.setOrgName(school.getSchoolName());
        sysOrg.setProvince(school.getProvince());
        sysOrg.setOrgType("2");
        sysOrg.setCity(school.getCity());
        sysOrg.setPid(school.getOrgId());
        ResultVO resultVO = sysOrgService.save(sysOrg);
        if (RespCode.SUCCESS.getCode() == resultVO.getCode()) {
            SysOrg data = (SysOrg) resultVO.getData();
            school.setId(data.getId());
            school.setOrgCode(data.getOrgCode());
        } else {
            return ResultVO.error(RespCode.ERROR);
        }
        schoolMapper.insert(school);
        return ResultVO.ok();
    }

    /**
     * @param school
     * @return
     */
    @Transactional
    @Override
    public ResultVO updateSchool(School school) {
        SysOrg sysOrg = new SysOrg();
        sysOrg.setId(school.getId());
        sysOrg.setOrgName(school.getSchoolName());
        sysOrg.setProvince(school.getProvince());
        sysOrg.setCity(school.getCity());
        sysOrgService.updateById(sysOrg);
        schoolMapper.updateById(school);
        return ResultVO.ok();
    }

    @Override
    public List<School> getSchoolByOrgCode(String orgCode) {
        return new LambdaQueryChainWrapper<>(schoolMapper)
                .likeRight(School::getOrgCode, orgCode)
                .list();
    }

    @Override
    public Map<Integer, String> getSchoolNamesBySchoolIds(String schoolIds) {
        List<Map<String, Object>> schoolNames = schoolMapper.getSchoolNamesBySchoolIds(schoolIds.split(","));
        return schoolNames.stream().collect(Collectors.toMap(item -> (Integer) item.get("id"), item -> (String) item.get("schoolName")));
    }

    @Override
    public School getSchoolInfoById(Integer id) {
        return schoolMapper.getSchoolInfoById(id);
    }

    @Override
    public School getSchoolInfoByOrgCode(String orgCode) {
        return schoolMapper.getSchoolInfoByOrgCode(orgCode);
    }


}
